package com.cheer.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.cheer.common.Page;
import com.cheer.common.PageInfo;
import com.cheer.common.Result;
import com.cheer.dto.OaLearnOrganDto;
import com.cheer.dto.OaRankQueryDTO;
import com.cheer.dto.OaUserQueryDTO;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.OaIntegralService;
import com.cheer.service.OaLearnEntryService;
import com.cheer.service.OaUserService;
import com.cheer.vo.*;
import io.swagger.annotations.ApiOperation;
import jodd.io.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/user")
public class OaUserController {

    @Autowired
    private UserInfoHolder userInfoHolder;

    @Autowired
    private OaUserService oaUserService;

    @Autowired
    private OaIntegralService oaIntegralService;

    @Autowired
    private OaLearnEntryService oaLearnEntryService;

    @ApiOperation("查询用户列表")
    @PostMapping("/list")
    public Result<Page<OaUserListVO>> list(@RequestBody OaUserQueryDTO oaUserQueryDTO) {
        if (oaUserQueryDTO.getPid() == null) {
            oaUserQueryDTO.setPid(userInfoHolder.getAdmin().getPid());
        }
        return Result.success(oaUserService.list(oaUserQueryDTO));
    }

    @ApiOperation("下载用户列表")
    @PostMapping("/download")
    public Result download(@RequestBody OaUserQueryDTO oaUserQueryDTO, HttpServletResponse response) {
        if (oaUserQueryDTO.getPid() == null) {
            oaUserQueryDTO.setPid(userInfoHolder.getAdmin().getPid());
        }
        oaUserService.download(oaUserQueryDTO, response);
        return Result.success();
    }

    @ApiOperation("查询用户详情")
    @GetMapping("/info")
    public Result<OaUserInfoVO> info(@RequestParam Integer id) {
        return Result.success(oaUserService.info(id));
    }

    @ApiOperation("查询用户积分记录")
    @GetMapping("/credit")
    public Result<OaCreditVO> credit(@RequestParam Integer id) {
        return Result.success(oaIntegralService.listByUserId(id));
    }

    //TODO 获取用户操作记录 排查问题时候使用
    @ApiOperation("获取用户操作记录")
    @GetMapping("/getRecord")
    public Result getRecord() {

        List<String> list = new ArrayList<>();
        try {
            // 1. 执行sql读取文件ID值
            String[] strings = FileUtil.readLines("C:\\Users\\Administrator\\Desktop\\dxxuserInfo.txt");
            // 2. 调用之后打印
            for (int i = 0; i <strings.length; i++) {

                String[] split = strings[i].split("\\s");
                Integer userId = Integer.valueOf(split[0]);
                OaCreditVO oaCreditVO = oaIntegralService.listByUserId(userId);
                for (int j = 0; j <oaCreditVO.getCreditList().size(); j++) {
                    OaCreditListVO oaCreditListVO = oaCreditVO.getCreditList().get(j);
                    if(oaCreditListVO.getName().equals("本期学习")){
                        Date parse = DateUtil.parse(oaCreditListVO.getTime(), "yyyy-MM-dd HH:mm:ss");
                        if(parse.compareTo(DateUtil.parse("2022-03-10","yyyy-MM-dd")) > 1){
                            list.add("用户名：" + split[1] + "\t操作：" +oaCreditListVO.getName() + "\t积分：" + oaCreditListVO.getCredit() + "\t操作时间："+oaCreditListVO.getTime());
                        }
                    }
                    //list.add("用户名：" + split[1] + "\t操作：" +oaCreditListVO.getName() + "\t积分：" + oaCreditListVO.getCredit() + "\t操作时间："+oaCreditListVO.getTime());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("-------------开始---------------");
        list.forEach(record -> System.out.println(record));
        System.out.println("-------------结束--------------");
        return Result.success();
    }

    //TODO
    @ApiOperation("获取颂词")
    @GetMapping("/getAudio")
    public Result getAudio() throws IOException {

        List<AudioVO> records = oaIntegralService.getAudio();
        // 查询机构 串联 ID值由小到大
        for (int i = 0; i < records.size(); i++) {
            AudioVO stringStringMap = records.get(i);
            String path = stringStringMap.getPath();
            String orgName = oaIntegralService.getAllOrgName(path);
            stringStringMap.setOrgName(orgName);
            if(i <= 10){
                System.out.println(stringStringMap.getName()+"\t"+stringStringMap.getType()+"\t"+stringStringMap.getVote()+"\t"+stringStringMap.getOrgName());
            }
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < records.size(); i++) {
            AudioVO  audioVO = records.get(i);
            sb.append(audioVO.getName()+"\t"+audioVO.getType()+"\t"+audioVO.getVote()+"\t"+audioVO.getOrgName() +"\n") ;
        }
        FileUtil.writeString(new File("C:\\Users\\Administrator\\Desktop\\报名表.txt"), sb.toString());
        return Result.success();
    }


    @ApiOperation("查询用户报名历史")
    @GetMapping("/history")
    public Result<List<OaHistoryListVO>> history(@RequestParam Integer id) {
        return Result.success(oaLearnEntryService.listByUserId(id));
    }

    @ApiOperation("查询用户学习排名")
    @PostMapping("/rank")
    public Result<Page<OaRankListVO>> rank(@RequestBody OaRankQueryDTO rankQueryDTO) {
        if (rankQueryDTO.getPid() == null) {
            rankQueryDTO.setPid(userInfoHolder.getAdmin().getPid());
        }
        return Result.success(oaUserService.rank(rankQueryDTO));
    }

    @ApiOperation("下载用户学习排名")
    @PostMapping("/rank/download")
    public Result downloadRank(@RequestBody OaRankQueryDTO rankQueryDTO, HttpServletResponse response) {
        if (rankQueryDTO.getPid() == null) {
            rankQueryDTO.setPid(userInfoHolder.getAdmin().getPid());
        }
        oaUserService.downloadRank(rankQueryDTO, response);
        return Result.success();
    }

    /**
     * 机关学习列表
     * @param organDto
     * @return
     */
    @GetMapping("getOrganList")
    @ApiOperation("机关学习列表")
    public Result<PageInfo<OaLearnOrgan>> getOrganList(OaLearnOrganDto organDto){
        return Result.success(oaUserService.getOrganList(organDto));

    }

    /**
     * 机关学习部门列表
     * @return
     */
    @GetMapping("getOrganDeptList")
    @ApiOperation("机关学习部门列表")
    public Result<List<String>> getOrganDeptList(){
        return Result.success(oaUserService.getOrganDeptList());
    }

    /**
     * 机关学习列表-下载
     * @param organDto
     * @param response
     * @return
     */
    @ApiOperation("机关学习列表-下载")
    @PostMapping("/organ/download")
    public Result downloadOrgan(OaLearnOrganDto organDto, HttpServletResponse response) {
        oaUserService.downloadOrgan(organDto, response);
        return Result.success();
    }
}
